
/**
 * 格式银行卡号
 * @param cardNumber
 * @returns {string}
 */
export function formatCardNumber(cardNumber) {
    let cardId = this.removeSpace(cardNumber);
    let cardStr = ''
    if (cardId.length > 4 && cardId.length <= 8) {
        cardStr = cardId.substring(0, 4) + ' ' + cardId.substring(4);
    } else if (cardId.length > 8 && cardId.length <= 12) {
        cardStr = cardId.substring(0, 4) + ' ' + cardId.substring(4, 8) + ' ' + cardId.substring(8);
    } else if (cardId.length > 12 && cardId.length <= 16) {
        cardStr = cardId.substring(0, 4) + ' ' + cardId.substring(4, 8) + ' ' + cardId.substring(8, 12) + ' ' + cardId.substring(12);
    } else if (cardId.length > 16) {
        cardStr = cardId.substring(0, 4) + ' ' + cardId.substring(4, 8) + ' ' + cardId.substring(8, 12) + ' ' + cardId.substring(12, 16) + ' ' + cardId.substring(16);
    } else {
        cardStr = cardId;
    }
    return cardStr;
}

/**
 * 格式身份证号
 * @param IDCard
 * @returns {string}
 */
export function formatIDCard(IDCard) {
    let IDCardNum = this.removeSpace(IDCard);
    let IDCardStr = '';
    if (IDCardNum.length > 6 && IDCardNum.length <= 14) {
        IDCardStr = IDCardNum.substring(0, 6) + ' ' + IDCardNum.substring(6);
    } else if (IDCardNum.length > 14 && IDCardNum.length <= 18) {
        IDCardStr = IDCardNum.substring(0, 6) + ' ' + IDCardNum.substring(6, 14) + ' ' + IDCardNum.substring(14);
    } else if (IDCardNum.length > 18) {
        IDCardStr = IDCardNum.substring(0, 6) + ' ' + IDCardNum.substring(6, 14) + ' ' + IDCardNum.substring(14, 18) + ' ' + IDCardNum.substring(18);
    } else {
        IDCardStr = IDCardNum;
    }
    return IDCardStr;
}

/**
 * 删除空格
 * @param stringWithSpace
 * @returns {XML|*|void|string}
 */
export function removeSpace(stringWithSpace) {
    return stringWithSpace ? stringWithSpace.replace(/\s+/g, '') : "";
}

/**
 * 分转元
 * @param amount
 * @returns {string}
 */
export function fentoyuan(amount) {
  var val = parseInt(amount) / 100;
    return val.toFixed(2);
}

/**
 * 元转分
 * @param amount
 * @returns {number}
 */
export function yuanToFen(amount) {
    return amount * 100;
}

/**
 * 设置缓存
 * @param key
 * @returns {*}
 */
export function setStorageSync(key, value) {
    if (key) {
        try {
            if (!value) {
                wx.removeStorageSync(key);
            } else {
                wx.setStorageSync(key, value);
            }
        } catch (e) {
        }
    }
}

/**
 * 同步获取缓存
 * @param key
 * @returns {*}
 */
export function getStorageSync(key) {
    try {
        let value = wx.getStorageSync(key);
        if (value) {
            return value;
        }
    } catch (e) {
    }
    return '';
}

/**
 * 获取随机数
 * @param length
 * @returns {string}
 */
export function randomNumber(length) {
    let randomNum = "";
    for (let i = 0; i < length; i++) {
        randomNum = randomNum + Math.floor(Math.random() * 10);
    }
    return randomNum;
}

/**
 * 从array中获取指定长度的随机串
 * @param array 指定列表
 * @param length 指定长度
 * @returns {string}
 */
export function getRandom(array, length) {
    var str = "",
        range = length,
        arr = array;

    for (var i = 0; i < range; i++) {
        let pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}

/**
 * 字符串截取月份
 * @param dateStr
 * @returns {number}
 */
export function getMonthWithDate(dateStr) {
    let formatDate = new Date(dateStr);
    return formatDate.getMonth() + 1;
}

/**
 * 获取最近n条
 * @param list
 * @param item
 * @param length
 * @returns {Array}
 */
export function getRecentList(list = [], item, length = 10) {
    if (item) {
        let index = list.indexOf(item);
        if (index < 0) {
            //记录中没有
            list.unshift(item);
            if (list.length > length) {
                //最近length条
                list = list.slice(0, length);
            }
        } else {
            //记录中有
            list.splice(index, 1);
            list.unshift(item);
        }
    }
    return list;
}
